#include "..\CookHeader.h"
#include <Windows.h>
#include <atlimage.h>
#include <conio.h>
CString filename = "c:/CookDataC/PNG/pet01.png";
const int ROW = 512, COL = 512;
COLORREF imageAry[ROW][COL];

void readImage() {
	CImage image;
	image.Load(filename);

	for (int i = 0; i < ROW; i++)
		for (int k = 0; k < COL; k++)
			imageAry[i][k] = image.GetPixel(k, i);
}

void printImage() {
	HWND hwnd;
	HDC hdc;
	hwnd = GetForegroundWindow();
	hdc = GetDC(hwnd);  // hdc = GetDC(NULL);

	for (int i = 0; i < ROW; i++)
		for (int k = 0; k < COL; k++) {
			int r = GetRValue(imageAry[i][k]);
			int g = GetGValue(imageAry[i][k]);
			int b = GetBValue(imageAry[i][k]);
			SetPixel(hdc, k + 100, i + 100, RGB(r, g, b));
		}
	ReleaseDC(hwnd, hdc);
}

int main() {
	_getch();
	readImage();

	Array <int> photoAry;
	for (int i = 0; i < ROW; i++)
		for (int k = 0; k < COL; k++) {
			int r = GetRValue(imageAry[i][k]);
			int g = GetGValue(imageAry[i][k]);
			int b = GetBValue(imageAry[i][k]);
			int value = (r + g + b) / 3;
			photoAry.push_back(value);
		}

	// GrayScale(256) --> BW Image(2)
	for (int i = 0; i < len(photoAry); i++) {
		if (photoAry[i] <= 127)
			photoAry[i] = 0;
		else 
			photoAry[i] = 255;
	}

	// imageAry BW 
	int pos = 0;
	for (int i = 0; i < ROW; i++)
		for (int k = 0; k < COL; k++) {
			int r, g, b;
			r = g = b = photoAry[pos];
			imageAry[i][k] = RGB(r, g, b);
			pos++;
		}
	printImage();
}